<!DOCTYPE html>
<html>
<head>
    <title>真实景点坐标获取工具</title>
</head>
<body>
<button onclick="return getCoordinates()">获取真实坐标</button>
<p>打开浏览器控制台查看结果</p>

<script>
    // 请替换为您在高德开放平台申请的实际API Key
    // 访问 https://console.amap.com/dev/key/app 查看 [Web服务]部分的[Key]
    const apiKey = "";

    // 景点数据
    const poiData = [
        //由于高德地图有调用频次限制,因此不要一次性获取全部坐标,分批次获取
        {
            "id": 51,
            "name": "治仙塔景区",
            "area": "北京市密云区",
            "price": "70元",
            "freeTickets": "免票1次",
            "serviceTime": "2026.1.1-12.31，团队无效，春节活动无效"
        },
        {
            "id": 52,
            "name": "盛世京城大剧院杂技",
            "area": "北京市昌平区",
            "price": "280元",
            "freeTickets": "免票1次",
            "serviceTime": "2025.9.10-2026.9.10，暑假需预约，中秋十一、五一假期无效，冬季停业"
        },
        {
            "id": 53,
            "name": "虎峪风景区",
            "area": "北京市昌平区",
            "price": "15元",
            "freeTickets": "免票不限次",
            "serviceTime": "2026.4.1-10.31，五一、十一假期无效"
        },
        {
            "id": 54,
            "name": "大杨山森林公园",
            "area": "北京市昌平区",
            "price": "15元",
            "freeTickets": "免票不限次",
            "serviceTime": "2026.5.1-10.31，冬季停业"
        },
        {
            "id": 55,
            "name": "大岭沟猕猴桃谷",
            "area": "北京市昌平区",
            "price": "30元",
            "freeTickets": "免票不限次",
            "serviceTime": "2026.4.1-10.31，法定假期无效"
        },
        // {
        //     "id": 56,
        //     "name": "袋鼠一二三亲子乐园",
        //     "area": "北京市昌平区",
        //     "price": "68元",
        //     "freeTickets": "免票2次",
        //     "serviceTime": "2025.9.1-10.31，2026.4.1-8.31，法定假期无效，六一当天无效"
        // },
        // {
        //     "id": 57,
        //     "name": "熊猫一二三亲子乐园",
        //     "area": "北京市昌平区",
        //     "price": "68元",
        //     "freeTickets": "免票2次",
        //     "serviceTime": "2025.9.1-10.31，2026.4.1-8.31，法定假期无效，六一当天无效"
        // },
        // {
        //     "id": 58,
        //     "name": "凤凰谷",
        //     "area": "北京市昌平区",
        //     "price": "58元",
        //     "freeTickets": "免票1次",
        //     "serviceTime": "2026.1.1-12.31"
        // },
        // {
        //     "id": 59,
        //     "name": "那时明月花溪谷",
        //     "area": "北京市昌平区",
        //     "price": "60元",
        //     "freeTickets": "免票不限次",
        //     "serviceTime": "2025.9.1-2026.8.31，团队无效，冬季营业时间待定"
        // },
    ];

    async function getCoordinates() {
        if (apiKey === ""){
            alert("请先访问 https://console.amap.com/dev/key/app 配置您的高德地图API KEY,记得是[Web服务]类型的!");
            return false;
        }

        console.log("开始获取真实景点坐标...");

        // 复制原始数据，避免修改原数组
        const resultData = JSON.parse(JSON.stringify(poiData));

        for (let i = 0; i < resultData.length; i++) {
            const poi = resultData[i];
            const address = `${poi.area} ${poi.name}`;

            try {
                // 使用JSONP方法获取坐标
                const location = await fetchWithJSONP(address);
                if (location) {
                    poi.location = location;
                    console.log(`✓ ${address}: ${location}`);
                } else {
                    console.log(`✗ ${address}: 未找到坐标`);
                }
            } catch (error) {
                console.log(`✗ ${address}: 请求失败 - ${error.message}`);
            }

            // 添加延迟避免请求过于频繁
            await new Promise(resolve => setTimeout(resolve, 200));
        }

        console.log("所有景点坐标获取完成:");
        console.log(JSON.stringify(resultData, null, 2));
    }

    // 使用JSONP方法获取坐标
    function fetchWithJSONP(address) {
        return new Promise((resolve) => {
            const callbackName = `jsonpCallback_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;

            window[callbackName] = function(response) {
                if (response.status === '1' && response.geocodes && response.geocodes.length > 0) {
                    resolve(response.geocodes[0].location);
                } else {
                    resolve(null);
                }

                // 清理
                delete window[callbackName];
                document.head.removeChild(script);
            };

            // 创建JSONP脚本
            const script = document.createElement('script');
            const encodedAddress = encodeURIComponent(address);
            script.src = `https://restapi.amap.com/v3/geocode/geo?address=${encodedAddress}&key=${apiKey}&output=jsonp&callback=${callbackName}`;
            document.head.appendChild(script);
        });
    }
</script>
</body>
</html>